﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;

public partial class Write : System.Web.UI.Page
{
    //public string ServerURL = "localhost:1588";
    public string ServerURL = "localhost:63451";
    protected void Page_Load(object sender, EventArgs e)
    {
        btnInsert.Attributes.Add("onclick", "return validate();");

    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        //1. DB INSERT Setting
        SqlConnection sqlConn = new SqlConnection();
        sqlConn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString;
        
        SqlCommand sqlCmd=new SqlCommand();
        sqlCmd.Connection=sqlConn;

        SqlParameter[] sqlPrm;

        /*
	        [UCCIdx] [int] IDENTITY(1,1) NOT NULL,
	        [UCCUserName] [nvarchar](500) NOT NULL,
	        [UCCFile] [varchar](500) NOT NULL,
	        [UCCTitle] [nchar](100) NULL,
	        [UCCContent] [ntext] NULL,
	        [UCCImage] [varchar](500) NOT NULL,
	        [UCCThumbnail] [varchar](500) NOT NULL,
	        [UCCStatus] [smallint] NOT NULL,
	        [UCCEncodingOption] [smallint] NOT NULL
         */

        hdnThumb.Value = "";
        hdnThumbList.Value = "";
        hdnStatus.Value = "0"; //인코딩 작업 체크하기  0 = 인코딩 대기, 1 = 인코딩 완, 2 = 인코딩 오류

        sqlPrm = new SqlParameter[8];
        sqlPrm[0] = new SqlParameter("@UCCUserName", txtUserName.Text);
        sqlPrm[1] = new SqlParameter("@UCCFile", hdnFileName.Value.Replace(String.Format("http://{0}", ServerURL), String.Empty));
        sqlPrm[2] = new SqlParameter("@UCCTitle", txtSubject.Text);
        sqlPrm[3] = new SqlParameter("@UCCContent", txtContent.Text.Replace("<", "&lt;"));
        sqlPrm[4] = new SqlParameter("@UCCImage", hdnThumb.Value.Replace(String.Format("http://{0}", ServerURL), String.Empty));
        sqlPrm[5] = new SqlParameter("@UCCThumbnail", hdnThumbList.Value.Replace(String.Format("http://{0}", ServerURL), String.Empty));
        sqlPrm[6] = new SqlParameter("@UCCStatus", hdnStatus.Value.Replace(String.Format("http://{0}", ServerURL), String.Empty));
        sqlPrm[7] = new SqlParameter("@UCCEncodingOption", hdnEncodingKind.Value.Replace(String.Format("http://{0}", ServerURL), String.Empty));

        string Query = @"INSERT [UCCLight](UCCUserName,UCCFile,UCCTitle,UCCContent,UCCImage,UCCThumbnail,UCCStatus,UCCEncodingOption)
                            Values(@UCCUserName,@UCCFile,@UCCTitle,@UCCContent,@UCCImage,@UCCThumbnail,@UCCStatus,@UCCEncodingOption)";

        foreach (SqlParameter param in sqlPrm)
        {
            sqlCmd.Parameters.Add(param);
        }
        sqlCmd.CommandText = Query;
        sqlConn.Open();
        sqlCmd.ExecuteNonQuery();
        sqlConn.Close();

        Response.Write("<script>alert('저장되었습니다. 서버에서 인코딩 작업을 끝낸 후에 동영상을 확인할 수 있습니다.');location.href='Default.aspx';</script>");
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        //Response.Redirect("List.aspx");
    }
}
